/**
 * 403错误
 * @param {*} url 
 * @param {*} data 
 * @returns 
 */
const tdt_jsonp = (url, data) => {
  if (!url) throw new Error('url is necessary');
  // const callback = 'CALLBACK' + Math.random().toString().substr(9, 18);
  const callback = 'jsonpcallback';
  const JSONP = document.createElement('script');
  JSONP.setAttribute('type', 'text/javascript');

  const headEle = document.getElementsByTagName('head')[0];

  let ret = '';
  if (data) {
    if (typeof data === 'string') {
      ret = '&' + data;
    } else if (typeof data === 'object') {
      for (let key in data) ret += '&' + key + '=' + encodeURIComponent(data[key]);
    }
    ret += '&_time=' + Date.now();
  }

  JSONP.src = `${url}?callback=${callback}${ret}`;

  function remove() {
    headEle.removeChild(JSONP);
    delete window[callback];
  }

  return new Promise((resolve, reject) => {
    window[callback] = r => {
      resolve(r);
      remove();
    };
    JSONP.onerror = function (e) {
      reject(e);
      remove();
    };
    headEle.appendChild(JSONP);
  });
};

export default tdt_jsonp;